package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.CookInfoAdapter;
import com.shijiwei.lifthelper.bean.CookBean;
import com.shijiwei.lifthelper.bean.CookData;
import com.shijiwei.lifthelper.bean.CookInfo;
import com.shijiwei.lifthelper.bean.DBCook;
import com.shijiwei.lifthelper.utils.DbUtils;
import com.shijiwei.lifthelper.utils.UrlConstants;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.onekeyshare.OnekeyShare;

@ContentView(R.layout.activity_cook_info)
public class CookInfoActivity extends AppCompatActivity {

    @ViewInject(R.id.tv_cook_info_title)
    private TextView titleText;
    @ViewInject(R.id.lv_cook_info)
    private ListView stepListView;

    private CookData data;
    private String category;
    private Callback.Cancelable get;

    private CookInfoAdapter infoAdapter;
    private List<CookData.StepsEntity> stepsEntityList = new ArrayList<>();

    private HeadView head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cook_info);
        x.view().inject(this);
        data = (CookData) getIntent().getSerializableExtra("data");
        category = getIntent().getStringExtra("category");
        initView();
        initData();
    }

    private void initData() {
        if (category != null){
            get = x.http().get(getRequestParams(), new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    LogUtil.e("--------CollectCook" + result);
                    if (result != null) {
                        CookBean cookBean = JSONObject.parseObject(result, CookBean.class);
                        if (cookBean != null) {
                            List<CookData> cookDatas = cookBean.getResult().getData();
                            data = cookDatas.get(0);
                            createData(data);
                        } else {
                            Toast.makeText(CookInfoActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                        }
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(CookInfoActivity.this, "网络异常，请求不到数据啊！！", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        } else {
            createData(data);
        }

    }
    private void createData(CookData data){
        head.cookImg.setImageResource(R.mipmap.umeng_socialize_share_pic);
//        x.image().bind(head.cookImg,data.getAlbums());
        Picasso.with(this).load(data.getAlbums()).into(head.cookImg);
        titleText.setText(data.getTitle());
        head.bodyText.setText(data.getImtro());
        StringBuffer sb = new StringBuffer();
        sb.append(data.getIngredients() + "\n");
        String[] strings = data.getBurden().split(";");
        for (int i = 0; i < strings.length; i++) {
            sb.append(strings[i]+"\n");
        }
        head.scText.setText(sb.toString());
        List<CookData.StepsEntity> steps = data.getSteps();
        stepsEntityList.addAll(steps);
        infoAdapter.notifyDataSetChanged();
    }

    private void initView() {
        head = new HeadView();

        infoAdapter = new CookInfoAdapter(stepsEntityList,this);

        stepListView.addHeaderView(head.headView);
        stepListView.setAdapter(infoAdapter);
    }

    private class HeadView{
        private View headView;

        @ViewInject(R.id.iv_cook_info_img)
        private ImageView cookImg;
        @ViewInject(R.id.tv_cook_info_body)
        private TextView bodyText;
        @ViewInject(R.id.tv_cook_info_sc)
        private TextView scText;

        public HeadView() {
            headView = getLayoutInflater().inflate(R.layout.item_listview_head_cook,null);
            x.view().inject(HeadView.this,headView);
        }

        @Event(value = {R.id.btn_cook_info_collect,R.id.btn_cook_info_share})
        private void onButtonClick(View view){
            switch (view.getId()){
                case R.id.btn_cook_info_collect:
                    LogUtil.e("-------CookInfo" + data.getTitle());
                    DBCook dbCook = new DBCook();
                    dbCook.setTitle(data.getTitle());
                    dbCook.setAlbums(data.getAlbums());
                    dbCook.setBurden(data.getBurden());
                    dbCook.setImtro(data.getImtro());
                    try {
                        DbUtils.getDb().saveOrUpdate(dbCook);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    Toast.makeText(CookInfoActivity.this,"收藏成功!!",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setAction("cook");
                    sendBroadcast(intent);
                    break;
                case R.id.btn_cook_info_share:
                    LogUtil.e("-------CookInfo"+data.getId());
                    OnekeyShare oks = new OnekeyShare();
                    oks.setDialogMode();
                    //关闭sso授权
                    oks.disableSSOWhenAuthorize();
                    oks.setText("我学会了制作" + data.getTitle() + "\n" + data.getImtro());
                    oks.setImageUrl(data.getAlbums());
                    oks.setTitleUrl(data.getAlbums());
                    oks.setViewToShare(head.cookImg);
                    oks.show(CookInfoActivity.this);
                    break;
            }
        }

    }

    @Event(value = {R.id.iv_cook_info_close})
    private void onViewClick(View view){
        switch (view.getId()){
            case R.id.iv_cook_info_close:
                finish();
                break;

        }
    }

    private RequestParams getRequestParams(){
        RequestParams params = new RequestParams(UrlConstants.COOKBOOK_URL_GET);
        params.addBodyParameter("menu",category);
        params.addBodyParameter("rn", "15");
        params.addBodyParameter("start", "0");
        return params;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (get != null) {
            get.cancel();
        }
    }
}
